﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DAQ.Common;
using System.Net;

namespace DAQ
{
    public partial class MainForm : MetroFramework.Forms.MetroForm
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void SETTING_Enter(object sender, EventArgs e)
        {
            this.cboPortName.Items.Clear();
            this.cboPortName.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
            this.cboPortName.Text = Config.GetConfig(CFGS.BTSR_COM, CFGS.PortName);
            cboPortName.SelectedIndexChanged += (s, c) =>
            {
                Config.SaveConfig(CFGS.BTSR_COM, CFGS.PortName, this.cboPortName.Text);
            };
            this.cboBaudRate.Text = Config.GetConfig(CFGS.BTSR_COM, CFGS.BaudRate);
            try
            {
                IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
                IPAddress[] addr = ipEntry.AddressList.Where(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork).ToArray();

                //  this.lblIP.Text = addr[0].ToString();
                if (addr != null && addr.Length > 0)
                {
                    cboip.Items.Clear();
                    cboip.Items.AddRange(addr);
                    cboip.Text = Config.GetConfig(CFGS.MODBUS, CFGS.IP);
                }
            }
            catch (Exception)
            {

                //   throw;
            }
            txtP1.Text = Config.GetConfig(CFGS.TENSIONS, CFGS.MAINTENSION_P1);
            txtP2.Text = Config.GetConfig(CFGS.TENSIONS, CFGS.MAINTENSION_P2);
            txtP3.Text = Config.GetConfig(CFGS.TENSIONS, CFGS.MAINTENSION_P3);
            txtP4.Text = Config.GetConfig(CFGS.TENSIONS, CFGS.MAINTENSION_P4);
            txtTE.Text = Config.GetConfig(CFGS.TENSIONS, CFGS.TENSION_ERROR);
        }

        private void metroButton1_Click(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();
            form1.Show();
        }

        private void metroToggle1_CheckedChanged(object sender, EventArgs e)
        {
            this.viewers1.EnableRead = this.metroToggle1.Checked;
        }



        private void metroComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

                string select = this.cboip.Items[this.cboip.SelectedIndex].ToString();
                Config.SaveConfig(CFGS.MODBUS, CFGS.IP, select);
                CWMCmn.Inst.CreateModbusSlave(select);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void MATERIALS_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            bool login = false;
        
            if (lblLogin.Text == "LOGIN")
            {
                var q = new qustion();
                if (q.ShowDialog() == DialogResult.OK)
                {
                    if (q.result == 0)
                    {
                        login = true;
                    }
                    else
                    {
                        login = false;
                    }
                }
            }
            else
            {
                login = false;
            }
            lblLogin.Text = login ? "LOGOUT" : "LOGIN";
            btnbobbin.Visible = login;
            btnwire.Visible = login;
            button3.Visible = login;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var regs = CWMCmn.Inst.DataStore.HoldingRegisters;
          
            if (txtNo.Text == "")
            {
                MessageBox.Show("FAUP No is Empty");
                return;
            }
            if (txtNo.Text.Length <= 16)
            {
                Config.SaveConfig(CFGS.MATERIALS, CFGS.NO, this.txtNo.Text);
                var array = txtNo.Text.ToCharArray();
                for (int i = 0; i < 8; i++)
                {
                    ushort v=0;
                    if (2 * i < array.Length)
                        v = array[2 * i];
                    else
                        v = 0x20;
                    if (2 * i + 1 < array.Length)
                        v += (ushort)((array[2 * i + 1] << 8) & 0xff00);
                    else
                        v += 0x2000;
                    regs[i+100] = v;
                }
                MessageBox.Show("Success");
            }
            else
            {
                MessageBox.Show("FAUP No Length is out of 16");
            }
        }

        private void metroTextBox1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            var regs = CWMCmn.Inst.DataStore.HoldingRegisters;
            if (txtbobbin.Text == "")
            {
                MessageBox.Show("black bobbin asm ref ID is Empty");
                return;
            }
            if (txtbobbin.Text.Length <= 16)
            {
                Config.SaveConfig(CFGS.MATERIALS, CFGS.ASM, this.txtbobbin.Text);
                var array = txtbobbin.Text.ToCharArray();
                for (int i = 0; i < 8; i++)
                {
                    ushort v = 0;
                    if (2 * i < array.Length)
                        v = array[2 * i];
                    else
                        v = 0x20;
                    if (2 * i + 1 < array.Length)
                        v += (ushort)((array[2 * i + 1] << 8) & 0xff00);
                    else
                        v += 0x2000;
                    regs[i+110] = v;
                }
                MessageBox.Show("Success");
            }
            else
            {
                MessageBox.Show("black bobbin asm ref ID Length is out of 16");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var regs = CWMCmn.Inst.DataStore.HoldingRegisters;
            if (txtwire.Text == "")
            {
                MessageBox.Show("copper wire ref id is Empty");
                return;
            }
            if (txtwire.Text.Length <= 16)
            {
                Config.SaveConfig(CFGS.MATERIALS, CFGS.WIRE, this.txtwire.Text);
                var array = txtwire.Text.ToCharArray();
                for (int i = 0; i < 8; i++)
                {
                    ushort v = 0;
                    if (2 * i < array.Length)
                        v = array[2 * i];
                    else
                        v = 0x20;
                    if (2 * i + 1 < array.Length)
                        v += (ushort)((array[2 * i + 1] << 8) & 0xff00);
                    else
                        v += 0x2000;
                    regs[i+120] = v;
                }
                MessageBox.Show("Success");
            }
            else
            {
                MessageBox.Show("copper wire ref id Length is out of 16");
            }
        }

        private void MATERIALS_Enter(object sender, EventArgs e)
        {
            this.txtbobbin.Text = Config.GetConfig(CFGS.MATERIALS, CFGS.ASM);
            this.txtNo.Text = Config.GetConfig(CFGS.MATERIALS, CFGS.NO);
            this.txtwire.Text = Config.GetConfig(CFGS.MATERIALS, CFGS.WIRE);
        }

        private void SETTING_Click(object sender, EventArgs e)
        {

        }

        private void metroButton2_Click(object sender, EventArgs e)
        {
            int num=0;
            int[] values = new int[5];
            if(!int.TryParse(txtP1.Text,out num))
            {
                MessageBox.Show(txtP1.Text + " is not integer");
               
            }
            values[0] = num;
            if (!int.TryParse(txtP2.Text, out num))
            {
                MessageBox.Show(txtP2.Text + " is not integer");        
            }
            values[1] = num;
            if (!int.TryParse(txtP3.Text, out num))
            {
                MessageBox.Show(txtP3.Text + " is not integer");

            }
            values[2] = num;
            if (!int.TryParse(txtP4.Text, out num))
            {
                MessageBox.Show(txtP4.Text + " is not integer");
            }
            values[3] = num;
            if (!int.TryParse(txtTE.Text, out num))
            {
                MessageBox.Show(txtP4.Text + " is not integer");
            }
            values[4] = num;
        }
    }
}
